import type { DailyModel } from '@/model/DailyModel'

export function calKdj(dailyList: Array<DailyModel>) {
  const rsv: Array<number> = []
  rsv.push(...[0, 0, 0, 0, 0, 0, 0, 0])
  dailyList.filter((daily, index) => index >= 8)
    .forEach((daily, index) => {
      let high9 = Number.MIN_VALUE
      let low9 = Number.MAX_VALUE
      dailyList.slice(index, index + 9)
        .forEach(daily => {
          high9 = Math.max(high9, daily.high)
          low9 = Math.min(low9, daily.low)
        })
      rsv.push((daily.close - low9) / (high9 - low9) * 100)
    })
  const outKList: Array<number> = []
  const outDList: Array<number> = []
  const outJList: Array<number> = []
  outKList.push(50)
  outDList.push(50)
  outJList.push(50)
  dailyList.slice(1)
    .forEach((daily, index) => {
      outKList.push(2 / 3 * outKList[index] + 1 / 3 * rsv[index + 1])
      outDList.push(2 / 3 * outDList[index] + 1 / 3 * outKList[index + 1])
      outJList.push(3 * outKList[index + 1] - 2 * outDList[index + 1])
    })
  return [outKList, outDList, outJList]
}
